package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.api.vo.toplist.TopListDetailVo;
import org.jeecg.modules.music.entity.MusListSong;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusTopList;
import org.jeecg.modules.music.entity.MusToplistSong;
import org.jeecg.modules.music.mapper.MusArtistMapper;
import org.jeecg.modules.music.mapper.MusTopListMapper;
import org.jeecg.modules.music.mapper.MusToplistSongMapper;
import org.jeecg.modules.music.service.IMusTopListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MusTopListServiceImpl extends ServiceImpl<MusTopListMapper, MusTopList> implements IMusTopListService {

    @Autowired
    private MusToplistSongMapper musToplistSongMapper;


    @Autowired
    private MusArtistMapper musArtistMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveArtist(MusTopList topList) {
        this.save(topList);
    }

    @Override
    public void editTopList(MusTopList topList) {
        this.baseMapper.updateById(topList);
    }

    @Override
    public void deleteTopList(String id) {
        this.removeById(id);
    }

    @Override
    public void deleteBatchTopList(String ids) {
        this.removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public IPage<MusSong> querySongDetailPage(Page<MusSong> page, String toplistId, MusSong songParams) {
        return this.baseMapper.querySongDetailPage(page,toplistId,songParams);
    }

    @Override
    public IPage<MusSong> queryNoSongDetailPage(Page<MusSong> page, String toplistId, MusSong songParams) {
        return this.baseMapper.queryNoSongDetailPage(page,toplistId,songParams);
    }

    @Override
    public void saveSongs(String toplistId, String songIds) {
        List<String> list = Arrays.stream(songIds.split(",")).map(String::valueOf).collect(Collectors.toList());
        for(String songId : list){
            MusToplistSong listSong = new MusToplistSong();
            listSong.setToplistId(toplistId);
            listSong.setId(UUID.randomUUID().toString().replace("-", ""));
            listSong.setSongId(songId);
            listSong.setCreateDate(new Date());
            this.musToplistSongMapper.insert(listSong);
        }
    }

    @Override
    public List<MusSong> querySongsByListId(String id) {
        List<MusSong> songs = baseMapper.querySongsDetail(id);
        songs.stream().map(MusSong->{
            if (MusSong.getArtistId()!=null){
                MusSong.setArtist(musArtistMapper.selectById(MusSong.getArtistId()));
            }
            return MusSong;
        }).collect(Collectors.toList());
        return songs;
    }

    @Override
    public void deleteTopListSong(String toplistId, String songId) {
        musToplistSongMapper.deleteListSong(toplistId,songId);
    }

    @Override
    public void deleteTopListSongBatch(String toplistId, String songIds) {
        List<String> list = Arrays.stream(songIds.split(",")).map(String::valueOf).collect(Collectors.toList());
        for(String songId : list){
            musToplistSongMapper.deleteListSong(toplistId,songId);
        }
    }

    @Override
    public List<TopListDetailVo> queryTopListAll(Integer type) {
        return baseMapper.queryToplistAll(type);
    }
}
